<script setup>
import { ref } from 'vue'

// const categorylist = [
//   { id: 1, name: '男装' },
//   { id: 2, name: '女装' },
//   { id: 3, name: '童装' },
//   { id: 4, name: '运动服' },
//   { id: 5, name: '内衣' },
//   { id: 6, name: '配饰' },
//   { id: 7, name: '鞋类' }
// ]

const activeName = ref('')
const handleClick = (tab) => {
  console.log('tab切换了',tab.props.name)
  disabled.value = false
  queryParams.value.pageNum = 1
  queryParams.value.categoryId = tab.props.name
  getGoodList()
}

import { getCategory } from '@/api/good'
const categorylist = ref([])
const getCategoryList = async () => {
  const res = await getCategory()
  console.log(res)
  categorylist.value = res.rows
}
getCategoryList()


const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  categoryId: ''
})
const total = ref(0);
const disabled = ref(false);
const goodlist = ref([])
import { getGoodListAPI } from '@/api/good'
const getGoodList = async () => {
  const res = await getGoodListAPI(queryParams.value)
  console.log(res)
  // goodlist.value = res.data.data
  goodlist.value = res.rows
}
getGoodList()
//加载更多
const load = async () => {
  console.log('加载更多')
  //获取下一页数据
  queryParams.value.pageNum++;
  const res = await getGoodListAPI(queryParams.value);
  total.value = res.total;
  goodlist.value = [...goodlist.value, ...res.rows];
  //加载完毕停止监听
  disabled.value = goodlist.value.length >= total.value; 
  console.log('disabled结果'+disabled.value);
}

</script>

<template>
  <div class="container">
  <div class="category-box">
  <div class="recommod">
    <h1>为你推荐</h1>
    <p>根据你的喜好为你推荐</p>
  </div>
    <!-- <ul>
      <li v-for="category in categorylist" :key="category.id">{{ category }}</li>
    </ul> -->
    <el-tabs v-model="activeName" class="category-tabs" @tab-click="handleClick">
      <el-tab-pane  label="全部" name="" ></el-tab-pane>
      <el-tab-pane 
        v-for="category in categorylist"
        :key="category.id" 
        :label="category.categoryName" 
        :name="category.id"
        >
      </el-tab-pane>
    </el-tabs>
  </div>
  <div class="home-product">
      <ul class="goods-list"
          v-infinite-scroll="load"
          :infinite-scroll-disabled="disabled">
        <li v-for="good in goodlist" :key="good.id">
          <RouterLink :to="{name: 'gooddetail', params: {goodid: good.id}}" class="goods-item" >
            <img v-img-lazy="good.picture"  alt="" />
            <p class="name ellipsis">{{ good.goodName }}</p>
            <p class="desc ellipsis">{{ good.description }}</p>
            <p class="price">&yen;{{ good.price }}</p>
          </RouterLink>
        </li>
      </ul>
    </div>  
  </div>

</template>

<style scoped lang='scss'>
  .category-box{
    margin-top: 20px;
    background: #fff;
    .recommod{
      display: flex;
      flex-wrap: wrap;
      padding: 20px 0 0 20px;
      align-items: flex-end; //底部对齐
      p{
        margin-left: 10px;
        color: #999;
      }
    }
    ul{
      padding: 10px;
      display: flex;
      flex-wrap: wrap;
    }
    li{
      padding: 10px;
      margin-right: 20px;
      font-size: 18px;
    }
    .category-tabs{
      padding: 10px;
    }
    /* 内容文字 */
  ::v-deep .el-tabs__item {
    padding: 20px !important; //内容与padding框的距离
    font-size: 18px !important; //更改选项卡标签的字体大小
  }
  }


  .home-product {
    background: #fff;
    margin: 20px 0 20px 0;
    display: flex;

    .cover {
      width: 240px;
      height: 610px;
      margin-right: 10px;
      position: relative;

      img {
        width: 100%;
        height: 100%;
      }

      .label {
        width: 188px;
        height: 66px;
        display: flex;
        font-size: 18px;
        color: #fff;
        line-height: 66px;
        font-weight: normal;
        position: absolute;
        left: 0;
        top: 50%;
        transform: translate3d(0, -50%, 0);

        span {
          text-align: center;

          &:first-child {
            width: 76px;
            background: rgba(0, 0, 0, 0.9);
          }

          &:last-child {
            flex: 1;
            background: rgba(0, 0, 0, 0.7);
          }
        }
      }
    }


    .goods-list {
      width: 1240px;
      display: flex;
      flex-wrap: wrap;

      li {
        width: 240px;
        height: 300px;
        margin-right: 10px;
        margin-bottom: 10px;

        &:nth-last-child(-n + 4) {
          margin-bottom: 0;
        }

        &:nth-child(5n) {
          margin-right: 0;
        }
      }
    }

    .goods-item {
      display: block;
      width: 240px;
      padding: 20px 30px;
      text-align: center;
      transition: all .5s;
      background: #fff;


      &:hover {
        transform: translate3d(0, -3px, 0);
        box-shadow: 0 3px 8px rgb(0 0 0 / 20%);
      }

      img {
        width: 160px;
        height: 160px;
      }

      p {
        padding-top: 10px;
      }

      .name {
        font-size: 16px;
      }

      .desc {
        color: #999;
        height: 29px;
      }

      .price {
        color: $priceColor;
        font-size: 20px;
      }
    }
  }

</style>